#include "logs.h"

void myMessageOutPut(QtMsgType type, const QMessageLogContext & context, const QString& msg){
    static QMutex mutex;
    mutex.lock();

    QByteArray localMsg = msg.toLocal8Bit();
    QString strOutStream = "";
    switch(type){
        case QtDebugMsg:
            strOutStream = QString("[Debug]   :%1[%2](%3)%4 - %5");
        break;
        case QtInfoMsg:
            strOutStream = QString("[Info]    :%1[%2](%3)%4 - %5");
        break;
        case QtWarningMsg:
            strOutStream = QString("[Warning] :%1[%2](%3)%4 - %5");
        break;
        case QtCriticalMsg:
            strOutStream = QString("[Critical]:%1[%2](%3)%4 - %5");
        break;
        case QtFatalMsg:
            strOutStream = QString("[Fatal]   :%1[%2](%3)%4 - %5");
        break;
    }
    strOutStream = strOutStream.arg(QDateTime::currentDateTime().date().toString("yyyyMMdd"))
            .arg(QString(context.file)).arg(QString(context.function)).arg(context.line).arg(QString(localMsg));
    QString log_file_name = QString("uselseenote.log");
    QFile logfile(log_file_name);
    logfile.open(QIODevice::WriteOnly | QIODevice::Append);
    if(strOutStream != ""){
        QTextStream logStream(&logfile);
        logStream<<strOutStream<<"\r\n";
    }
    logfile.flush();
    logfile.close();
    mutex.unlock();
}
